<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input
            v-model="deptName"
            placeholder="请输入部门名称"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container">
          <el-tree
            :data="deptOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            default-expand-all
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>
      <!--台站数据-->
      <el-col :span="20" :xs="24">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="85px">
          <el-form-item label="设备名称" prop="equipmentName">
            <el-input
              v-model="queryParams.equipmentName"
              placeholder="请输入设备名称"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="设备类型" prop="equipmentType">
            <el-select v-model="queryParams.equipmentType" placeholder="请选择设备类型" clearable>
              <el-option
                v-for="dict in dict.type.cz_equipment_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="设备编号" prop="sn">
            <el-input
              v-model="queryParams.sn"
              placeholder="请输入设备编号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="陀螺仪编号" prop="gyroscopeCode">
            <el-input
              v-model="queryParams.gyroscopeCode"
              placeholder="请输入陀螺仪编号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['baseinfo:equipment:add']"
            >新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['baseinfo:equipment:edit']"
            >修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['baseinfo:equipment:remove']"
            >删除</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="equipmentList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="编号" align="center" prop="id" />
          <el-table-column label="设备名称" align="center" prop="equipmentName" />
          <el-table-column label="设备类型" align="center" prop="equipmentType">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.cz_equipment_type" :value="scope.row.equipmentType"/>
            </template>
          </el-table-column>
          <el-table-column label="归属部门" align="center" prop="deptName" />
          <el-table-column label="设备识别号" align="center" prop="sn" >
            <template slot-scope="scope">
              <span v-if="scope.row.equipmentType==1">{{scope.row.sn}}</span>
            </template>
          </el-table-column>
          <el-table-column label="陀螺仪编号/设备编号" align="center" prop="gyroscopeCode" />
          <el-table-column label="设备温度" align="center" prop="temperature" />
          <el-table-column label="电池电压" align="center" prop="voltage" />
          <el-table-column label="采样间隔" align="center" prop="sampleInterval" >
            <template slot-scope="scope">
              <span>{{ scope.row.sampleInterval}} {{scope.row.equipmentType==1?'秒':'分钟'}}</span>
            </template>
          </el-table-column>

          <el-table-column label="设备安装时间" align="center" prop="installTime" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.installTime, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['baseinfo:equipment:edit']"
              >修改</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['baseinfo:equipment:remove']"
              >删除</el-button>
              <el-button v-if="scope.row.equipmentType==2"
                size="mini"
                type="text"
                icon="el-icon-setting"
                @click="handleTempCorrect(scope.row)"
                v-hasPermi="['baseinfo:equipment:edit']"
              >温度校准</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <!-- 添加或修改设备管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="设备名称" prop="equipmentName">
          <el-input v-model="form.equipmentName" placeholder="请输入设备名称"/>
        </el-form-item>
        <el-form-item label="设备类型" prop="equipmentType">
          <el-select v-model="form.equipmentType" placeholder="请选择设备类型" clearable>
            <el-option
              v-for="dict in dict.type.cz_equipment_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="归属部门" prop="deptId">
          <treeselect v-model="form.deptId" :options="deptOptions" :show-count="true" placeholder="请选择归属部门" />
        </el-form-item>
        <el-form-item label="设备识别号" prop="sn" v-if="form.equipmentType==1">
          <el-input v-model="form.sn" placeholder="请输入设备识别号" />
        </el-form-item>
        <el-form-item label="陀螺仪编号" prop="gyroscopeCode" v-if="form.equipmentType==1">
          <el-input v-model="form.gyroscopeCode" placeholder="请输入陀螺仪编号" />
        </el-form-item>
        <el-form-item label="设备编号" prop="gyroscopeCode" v-if="form.equipmentType==2">
          <el-input v-model="form.gyroscopeCode" placeholder="请输入陀螺仪编号" />
        </el-form-item>
        <el-form-item label="采样间隔" prop="sampleInterval" v-if="form.equipmentType==2">
          <el-input v-model="form.sampleInterval" placeholder="请输入设备采样间隔" style="width: 50%;"/>分钟
          <span style="color: red;">（*请注意时间单位）</span>
        </el-form-item>
        <el-form-item label="设备安装时间" prop="installTime">
          <el-date-picker clearable size="small"
            v-model="form.installTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择设备安装时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="温度修正系数设置" :visible.sync="open1" width="700px" append-to-body>
      <el-form ref="form1" :model="form1" :rules="rules1" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="设备名称:" prop="equipmentName">
              <el-input v-model="form1.equipmentName" readonly/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="测量深度:" prop="select">
              <el-select v-model="form1.select" placeholder="请选择深度" clearable>
                <el-option
                  v-for="dict in deeps"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
            <el-col :span="12">
              <el-form-item label="测量温度1:" prop="t11">
                <el-input v-model="form1.t11" placeholder="请输入"/>
              </el-form-item>
            </el-col>
          <el-col :span="12">
            <el-form-item label="标定温度1:" prop="t1">
              <el-input v-model="form1.t1" placeholder="请输入"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="测量温度2:">
              <el-input v-model="form1.t22" placeholder="请输入"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="标定温度2:">
              <el-input v-model="form1.t2" placeholder="请输入"/>
            </el-form-item>
          </el-col>

        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm1">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listEquipment, getEquipment, delEquipment, addEquipment, updateEquipment } from "@/api/baseinfo/equipment";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {treeselect} from "../../../api/system/dept";
import {getTempCorrectByCode,updateEquipmentCorrect} from "@/api/baseinfo/correct";

export default {
  name: "Equipment",
  dicts: ['cz_equipment_type'],
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      deeps:[
        {label:"5m",value:5},
        {label:"10m",value:10},
        {label:"15m",value:15},
        {label:"20m",value:20},
        {label:"25m",value:25},
        {label:"30m",value:30},
        {label:"35m",value:35},
        {label:"40m",value:40},
        {label:"45m",value:45},
        {label:"50m",value:50}
      ],
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备管理表格数据
      equipmentList: [],
      // 弹出层标题
      title: "",
      // 部门树选项
      deptOptions: undefined,
      // 部门名称
      deptName: undefined,
      // 是否显示弹出层
      open: false,
      open1: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        equipmentName: null,
        equipmentType: null,
        deptId: null,
        sn: null,
        gyroscopeCode: null,
      },
      // 表单参数
      form: {},
      form1: {},
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 表单校验
      rules: {
        equipmentName: [
          {required: true, message: "设备名称不能为空", trigger: "blur"}
        ],
        equipmentType: [
          {required: true, message: "设备类型不能为空", trigger: "blur"}
        ],
        deptId: [
          {required: true, message: "归属部门不能为空", trigger: ["blur", "change"]}
        ],
        sn: [
          {required: true, message: "设备编号不能为空", trigger: "blur"}
        ],
        gyroscopeCode: [
          {required: true, message: "陀螺仪编号不能为空", trigger: "blur"}
        ],
        installTime: [
          {required: true, message: "安装时间不能为空", trigger: "blur"}
        ],
        sampleInterval: [
          {required: true, message: "采样间隔不能为空", trigger: "blur"}
        ]
      },
      rules1: {
        select: [
          {required: true, message: "深度不能为空", trigger: "change"}
        ],
        t1: [
          {required: true, message: "温度不能为空", trigger: "blur"}
        ],
        t11: [
          {required: true, message: "温度不能为空", trigger: "blur"}
        ]
      }
    }
  },
  watch: {
    // 根据名称筛选部门树
    deptName(val) {
      this.$refs.tree.filter(val);
    }
  },
  created() {
    this.getTreeselect();
    this.getList();
  },
  methods: {
    /** 查询设备管理列表 */
    getList() {
      this.loading = true;
      listEquipment(this.queryParams).then(response => {
        this.equipmentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询部门下拉树结构 */
    getTreeselect() {
      treeselect().then(response => {
        this.deptOptions = response.data;
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.deptId = data.id;
      this.handleQuery();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.open1 = false;
      this.reset();
      this.reset1();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        equipmentName: null,
        equipmentType: null,
        deptId: null,
        sn: null,
        gyroscopeCode: null,
        lat: null,
        lon: null,
        freeMemory: null,
        gpsState: null,
        satelliteNum: null,
        temperature: null,
        voltage: null,
        sampleInterval: null,
        installTime: null
      };
      this.resetForm("form");
    },
    reset1() {
      this.form1 = {
        id: null,
        equipmentCode:null,
        correct5:null,
        correct10:null,
        correct15:null,
        correct20:null,
        correct25:null,
        correct30:null,
        correct35:null,
        correct40:null,
        correct45:null,
        correct50:null,
        select:null,
        t1:null,
        t11:null,
        t2:null,
        t22:null
      };
      this.resetForm("form1");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.deptId =null;
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加设备管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getEquipment(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改设备管理";
      });
    },
    handleTempCorrect(row){
      this.reset1();
      const code = row.gyroscopeCode;
      this.form1.equipmentCode = code;
      this.form1.equipmentName = row.equipmentName;
      this.open1 = true;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateEquipment(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addEquipment(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    submitForm1() {
      this.$refs["form1"].validate(valid => {
        if (valid) {
          let t;
          t = this.form1.t11-this.form1.t1;
          if(this.form1.t22 != null && this.form1.t2 != null) {
            let t1 = this.form1.t22 - this.form1.t2;
            t = (t + t1) / 2;
          }
          getTempCorrectByCode(this.form1.equipmentCode).then(res =>{
             let f = res.data;
             this.form1.id = f.id;
            switch (this.form1.select){
              case 5:
                this.form1.correct5 = f.correct5 + t;
                break;
              case 10:
                this.form1.correct10 = f.correct10 + t;
                break;
              case 15:
                this.form1.correct15 = f.correct15 + t;
                break;
              case 20:
                this.form1.correct20 = f.correct20 + t;
                break;
              case 25:
                this.form1.correct25 = f.correct25 + t;
                break;
              case 30:
                this.form1.correct30 = f.correct30 + t;
                break;
              case 35:
                this.form1.correct35 = f.correct35 + t;
                break;
              case 40:
                this.form1.correct40 = f.correct40 + t;
                break;
              case 45:
                this.form1.correct45 = f.correct45 + t;
                break;
              case 50:
                this.form1.correct50 = f.correct50 + t;
                break;
            };
            updateEquipmentCorrect(this.form1).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open1 = false;
            });
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除设备管理编号为"' + ids + '"的数据项？').then(function() {
        return delEquipment(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    }
  }
};
</script>
